package top.stmo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import top.stmo.mapper.LoginMapper;
import top.stmo.mapper.UserMapper;
import top.stmo.pojo.Login;
import top.stmo.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 *              Mapper 代理开发
 */
public class MybatisTest2 {

    public static void main(String[] args) throws IOException {

        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new
                SqlSessionFactoryBuilder().build(inputStream);

        //获取sqlSessionFactory对象,用他来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行sql

        //获取Mapper接口的代理对象
        LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class);
        List<Login> all = loginMapper.findAll();

        System.out.println(all);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.findById(1);
        System.out.println(user);

        //释放资源
        inputStream.close();
    }

}
